/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package upgmadendro.graph;

import java.util.ArrayList;

/**
 *
 * @author Shadev
 */
public class DistanceGraph {

    private ArrayList<Edge> edges = new ArrayList<Edge>();
    private ArrayList<Vertex> vertice = new ArrayList<Vertex>();

    public int getMinEdgeIndex() {
        int res = 0;

        double min = Double.MAX_VALUE;
        for (int i = 0; i < this.getEdges().size(); i++) {
            double val = this.getEdges().get(i).getValue();
            if (val < min) {
                min = val;
                res = i;
            }
        }
        return res;
    }

    public void printGraph(){
        for(Vertex v : this.getVertice()){
                    System.out.println("Vertex : "+v.getName());
                    for(Edge e : v.getEdges()){
                        System.out.println(" "+e.getValue()+";");
                    }
                    System.out.println("");
                }
    }

    public void updateEdges(Vertex toStay, Vertex toRemove) {
        Edge tempEdge = null;
        Vertex tempVertex = null;
        for (Edge e : toStay.getEdges()) {
            if (e.getVertice()[0] != toStay) {
                tempVertex = e.getVertice()[0];
            } else {
                tempVertex = e.getVertice()[1];
            }
            tempEdge = tempVertex.getEdgeToVertex(toRemove);
            e.setValue((e.getValue() + tempEdge.getValue()) / 2);
        }
    }

    public void removeVertex(Vertex v) {
        for (Edge e : v.getEdges()) {
            this.getEdges().remove(e);
            if (e.getVertice()[0] != v) {
                e.getVertice()[0].getEdges().remove(e);
            } else {
                e.getVertice()[1].getEdges().remove(e);
            }
        }
        this.getVertice().remove(v);
    }

    /**
     * @return the edges
     */
    public ArrayList<Edge> getEdges() {
        return edges;
    }

    /**
     * @return the vertice
     */
    public ArrayList<Vertex> getVertice() {
        return vertice;
    }
}
